package com.developer.platform.application.query;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.developer.platform.RedisConstant;
import com.developer.platform.application.query.assembler.DictAssembler;
import com.developer.platform.application.query.dto.DictDataCutDto;
import com.developer.platform.application.query.dto.DictDataDto;
import com.developer.platform.application.query.dto.DictTypeListDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.domain.DictData;
import com.developer.platform.domain.DictType;
import com.developer.platform.infrastructure.data.mapper.DictDataMapper;
import com.developer.platform.infrastructure.data.mapper.DictTypeMapper;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import com.veeker.redis.utils.RedisUtil;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 *
 * @author ：qiaoliang
 * @date ：2020-10-28
 */
@Component
public class DictQueryManager {

    private final DictTypeMapper dictTypeMapper;
    private final DictDataMapper dictDataMapper;
    private final DictAssembler dictAssembler;

    public DictQueryManager(DictTypeMapper dictTypeMapper,
                            DictDataMapper dictDataMapper,
                            DictAssembler dictAssembler) {
        this.dictTypeMapper = dictTypeMapper;
        this.dictDataMapper = dictDataMapper;
        this.dictAssembler = dictAssembler;
    }

    /**
     * 查询全局字典
     *
     * @author ：qiaoliang

     * @return java.util.Map<java.lang.String, java.util.List < com.microorganism.platform.application.query.dto.DictDataCutDto>>
     * @date 2020-10-28 16:05
     */
    public Map<String, List<DictDataCutDto>> overallDict(){

        List<Object> objects = RedisUtil.listGet(RedisConstant.DICT_ALL_KEYS);
        List<DictData> dictDataList;
        if(CollUtil.isNotEmpty(objects)){
            dictDataList = objects.stream().map(o->(DictData)o).collect(Collectors.toList());
        }else {
            LambdaQueryWrapper<DictData> dataQueryWrapper = new LambdaQueryWrapper<>();
            dataQueryWrapper.orderByAsc(DictData::getSort);
            dataQueryWrapper.eq(DictData::getStatus, StatusEnum.Y);
            dictDataList = dictDataMapper.selectList(dataQueryWrapper);
            if(CollUtil.isNotEmpty(dictDataList)){
                RedisUtil.listSet(RedisConstant.DICT_ALL_KEYS, Arrays.asList(dictDataList.toArray()));
            }
        }
        return dictAssembler.toDictDataCutDto(dictDataList);
    }


    /**
     * 分页获取字典类型
     *
     * @author ：qiaoliang
     * @param name : 字典类型名称
     * @param type :  字典类型
     * @param status : 字典类型状态
     * @param pageable : 分页信息
     * @return com.microorganism.platform.application.query.dto.ResponseModel<com.microorganism.platform.application.query.dto.DictTypeListDto>
     * @date 2020-11-09 16:55
     */
    public ResponseModel<DictTypeListDto> page(String name, String type , StatusEnum status , Pageable pageable){
        LambdaQueryWrapper<DictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(name), DictType::getName, name);
        queryWrapper.eq(StrUtil.isNotBlank(type), DictType::getType, type);
        queryWrapper.eq(Objects.nonNull(status), DictType::getStatus, status);
        return dictAssembler.toResponseModel(
                dictTypeMapper.selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()), queryWrapper));
    }

    /**
     * 通过字典类型查询字典数据
     *
     * @author ：qiaoliang
     * @param type :
     * @return java.util.List<com.microorganism.platform.application.query.dto.DictDataDto>
     * @date 2020-11-09 16:55
     */
    public List<DictDataDto> dataList(String type){
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictData::getType, type);
        queryWrapper.orderByAsc(DictData::getSort);
        return dictAssembler.toDictDataDto(dictDataMapper.selectList(queryWrapper));
    }

}
